package com.opc.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.opc.dto.LineStatus;
import com.opc.dto.SelectData;
import com.opc.entity.Device;
import com.opc.entity.DeviceRecordLatest;
import com.opc.service.DeviceRecordLatestService;
import com.opc.service.DeviceService;
import com.opc.utils.ApiResult;
import com.opc.utils.RedisUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author 作者
 * @since 2023-03-13
 */
@Slf4j
@RestController
@RequestMapping("/device")
@CrossOrigin("*")
public class DeviceController {
    @Resource
    private DeviceService deviceService;
    @Resource
    private RedisUtils redisUtils;

    @Resource
    private DeviceRecordLatestService recordLatestService;

    @GetMapping("device_id")
    public Device findDeviceById(@RequestParam(name = "id") Integer id) {
        Device device = deviceService.getById(id);
        return device;
    }

    @GetMapping("device_read_excel")
    public Device deviceReadExcel(@RequestParam(name = "id") Integer id) {
        Device device = deviceService.getById(id);
        return device;
    }

    @GetMapping("syc_redis")
    public void sycRedis() {
        QueryWrapper<Device> w = new QueryWrapper();
        w.eq("is_delete", 0);
        List<Device> list = deviceService.list(w);
        for (Device device : list) {
            redisUtils.remove(device.getCountKey());
            redisUtils.remove(device.getSetKey());
            redisUtils.hmSet(device.getCountKey(), "device_code", device.getDeviceCode());
            redisUtils.hmSet(device.getSetKey(), "device_code", device.getDeviceCode());
            redisUtils.hmSet(device.getCountKey(), "val", 0);
            redisUtils.hmSet(device.getSetKey(), "val", 0);
            redisUtils.hmSet(device.getCountKey(), "type", 1);
            redisUtils.hmSet(device.getSetKey(), "type", 0);
            redisUtils.lPush("key_list", device.getCountKey());
            redisUtils.lPush("key_list", device.getSetKey());
        }
        log.info("success");
    }

    @GetMapping("get_product_line")
    public ApiResult getProductLine() {
        return ApiResult.success(deviceService.getProductLine());
    }

    @GetMapping("get_record_List_pages")
    public ApiResult getRecordListByLinePages(@RequestParam(name = "proLine") String proLine,
                                              @RequestParam(name = "pageNo", defaultValue = "1") String pageNo,
                                              @RequestParam(name = "pageSize", defaultValue = "100") String pageSize) {
        log.info("proLine:{},pageNo:{},pageSize:{}", proLine, pageNo, pageSize);
        return ApiResult.success(deviceService.getRecordListByLine(proLine, Integer.parseInt(pageNo), Integer.parseInt(pageSize)));
    }

    @GetMapping("get_record_List")
    public ApiResult getRecordListByLine(@RequestParam(name = "proLine") String proLine) {
        log.info("proLine:{}", proLine);
        return ApiResult.success(deviceService.getRecordListByLineNoPages(proLine));
    }

    @GetMapping("get_device_list")
    public ApiResult getDeviceListByConditions(@RequestParam(name = "proLine") String proLine,
                                               @RequestParam(name = "stationNum") String stationNum) {
        log.info("proLine:{},stationNum:{}", proLine, stationNum);
        return ApiResult.success(deviceService.getDeviceListByConditions(proLine, stationNum));
    }


    @GetMapping("syc_redis_count_set")
    public void syc_redis_count_set() {
        QueryWrapper<Device> w = new QueryWrapper();
        w.eq("is_delete", 0);
        List<Device> devices = deviceService.list(w);
        Map<String, Device> map = new HashMap<>();
        for (Device device : devices) {
            map.put(device.getDeviceCode(), device);
        }
        List<DeviceRecordLatest> list = recordLatestService.selectAllRecords();
        for (DeviceRecordLatest deviceRecordLatest : list) {
            Device device = map.get(deviceRecordLatest.getDeviceCode());
            if (deviceRecordLatest.getValType() == 0) {
                redisUtils.hmSet(device.getSetKey(), "val", deviceRecordLatest.getVal());
            } else if (deviceRecordLatest.getValType() == 1) {
                redisUtils.hmSet(device.getCountKey(), "val", deviceRecordLatest.getVal());
            }
        }
    }

    @GetMapping("/line_status")
    public ApiResult getLineStatus() {
        Map<String, Integer> map = new ConcurrentHashMap<>();
        List<SelectData> productLine = deviceService.getProductLine();
        List<LineStatus> list = new ArrayList<>();
        for (SelectData selectData : productLine) {
            LineStatus lineStatus = new LineStatus();
            lineStatus.setName(selectData.getName());
            lineStatus.setValue(Integer.parseInt(String.valueOf(redisUtils.get(selectData.getValue()))));
            list.add(lineStatus);
        }
        return ApiResult.success(list);
    }

}









